/**
 * 微信小程序文件上传接口
 */

// 上传响应接口
export interface UploadResponse {
  success: boolean;
  message?: string;
  url?: string;
  filename?: string;
  size?: number;
  [key: string]: any;
}

// 上传参数接口
export interface UploadOptions {
  filePath: string;
  name?: string;
  type?: string;
  onProgress?: (progress: number) => void;
}

/**
 * 上传文件到指定服务器（微信小程序版本）
 * @param options 上传参数
 * @returns Promise<UploadResponse>
 */
export const uploadFile = (options: UploadOptions): Promise<UploadResponse> => {
  const { filePath, name, type } = options;
  console.log('object', options)
  return new Promise((resolve, reject) => {
    const fileName = name || `upload_${Date.now()}`;
    const uploadUrl = `https://upload.lihk180.dpdns.org/${encodeURIComponent(fileName)}`;
    
    uni.uploadFile({
      url: uploadUrl,
      filePath: filePath,
      name: 'file',
      method: 'PUT',
      header: {
        'Content-Type': 'application/json',
        'X-Custom-Auth-Key': 'lihklihk'
      },
      success: (res) => {
        const data = JSON.parse(res.data);
        resolve({
          success: true,
          message: '上传成功',
          ...data
        });
      },
      fail: reject
    });
  });
};
